<template>
  <el-dialog v-model="visible" :title="!dataForm.id ? $t('add') : $t('update')" :close-on-click-modal="false" :close-on-press-escape="false">
    <el-form :model="dataForm" :rules="dataRule" ref="dataFormRef" @keyup.enter="dataFormSubmitHandle()" label-width="160px">
      <el-form-item prop="name" label="库房名称：">
        <el-input v-model="dataForm.name" placeholder="请输入库房名称"></el-input>
      </el-form-item>
      <el-form-item prop="area" label="库房面积：">
        <el-input v-model="dataForm.area" placeholder="请输入库房面积"></el-input>
      </el-form-item>
      <el-form-item prop="dz" label="库房地址：">
        <region v-model="dataForm.dz"></region>
      </el-form-item>
      <el-form-item prop="address" label="详细地址">
        <el-input v-model="dataForm.address" placeholder="请输入库房详细地址"></el-input>
      </el-form-item>
      <div v-show="!dataForm.id">
        <el-form-item prop="intensiveNumber" label="密集架组数：">
          <el-input v-model="dataForm.intensiveNumber" placeholder="请输入"></el-input>
        </el-form-item>
        <el-form-item prop="singleValue" label="单排参数：">
          <span> 密集架共 </span>
          <el-input v-model="dataForm.layerNo" style="width: 15%"></el-input>
          <span> 层，每层 </span>
          <el-input v-model="dataForm.columnNo" style="width: 15%"></el-input>
          <span> 阁，每阁宽度为 </span>
          <el-input v-model="dataForm.signCapacity" style="width: 15%"></el-input>
        </el-form-item>
        <el-form-item prop="shelvingNoBeginRule" label="密集架编号起始值：">
          <el-input v-model="dataForm.shelvingNoBeginRule" placeholder="A-01"></el-input>
          <span style="color: #ccc">后续密集架编号递增顺序编号，如A-01，A-02，A-03，A-04,必须以-分隔等</span>
        </el-form-item>
      </div>
      <el-form-item prop="contactName" label="负责人：">
        <el-input v-model="dataForm.contactName" placeholder="请输入"></el-input>
      </el-form-item>
      <el-form-item prop="phone" label="联系电话：">
        <el-input v-model="dataForm.phone" placeholder="请输入"></el-input>
      </el-form-item>
    </el-form>
    <template v-slot:footer>
      <el-button @click="visible = false">{{ $t("cancel") }}</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">{{ $t("confirm") }}</el-button>
    </template>
  </el-dialog>
</template>

<script lang="ts">
  import region from "@/components/region/index.vue";
import { defineComponent, reactive } from "vue";
import baseService from "@/service/baseService";
import { useDebounce } from "@/utils/utils";
  import { IObject } from "@/types/interface"; // isEmail, isMobile, isNumber,
// import { IObject } from "@/types/interface";
export default defineComponent({
  components:{
    region
  },
  setup() {

    return reactive( {
      l:[] as any[],
      visible: false,
      dataForm: {
        id: "",
        dz:[],
        code: "",
        name: "",
        area: "",
        address: "",
        intensiveNumber: "",
        shelvingNoBeginRule: "",
        contactName: "",
        contactDistrict: "",
        phone: "",
        contactProvince: "",
        contactCity: "",
        layerNo: "",
        columnNo: "",
        signCapacity: ""
      }
    });
  },
  computed: {
    dataRule() {
      return {
        code: [{ required: true, message: this.$t("validate.required"), trigger: "blur" }],
        name: [{ required: true, message: this.$t("validate.required"), trigger: "change" }],
        layerNo: [{ required: true, message: this.$t("validate.required"), trigger: "change" }],
        columnNo: [{ required: true, message: this.$t("validate.required"), trigger: "change" }],
        signCapacity: [{ required: true, message: this.$t("validate.required"), trigger: "change" }],
        shelvingNoBeginRule: [{ required: true, message: this.$t("validate.required"), trigger: "change" }],
        dz: []
      };
    }
  },
  created() {
    this.dataFormSubmitHandle = useDebounce(this.dataFormSubmitHandle);
  },
  methods: {
    init() {
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataFormRef"].resetFields();
        this.getInfo();
      });
    },

    getInfo() {
      if (this.dataForm.id != "") {
        baseService.get(`/archive/warehouse/${this.dataForm.id}`).then((res) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg);
          }
          this.dataForm = {
            ...this.dataForm,
            dz:[res.data.contactProvince,res.data.contactCity,res.data.contactDistrict],
            ...res.data
          };
          // this.l.push(res.data.contactProvince)
          // this.l.push(res.data.contactCity)
          // this.l.push(res.data.contactDistrict)
        });
      }
    },
    dataFormSubmitHandle() {
      this.dataForm.contactProvince = this.l[0]
      this.dataForm.contactCity = this.l[1]
      this.dataForm.contactDistrict = this.l[2]
      this.$refs["dataFormRef"].validate((valid: boolean) => {
        if (!valid && !this.dataForm.id) {
          return false;
        }
        (!this.dataForm.id ? baseService.post : baseService.put)("/archive/warehouse", {
          ...this.dataForm
        }).then((res) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg);
          }
          this.$message({
            message: this.$t("prompt.success"),
            type: "success",
            duration: 500,
            onClose: () => {
              this.visible = false;
              this.$emit("refreshDataList");
            }
          });
        });
      });
    }
  }
});
</script>

<style lang="less">
.mod-tenant__tenant {
  .role-list {
    .el-select {
      width: 100%;
    }
  }
}

.el-dialog__body {
  width: 91%;
}

.plus_one {
  position: absolute;
  top: 1px;
  right: -45px;
}

// }
</style>
